package com.weixin.po;

import java.sql.Timestamp;
import java.util.Calendar;

/**
 * 微信通用接口凭证
 * 
 * @author wang
 * 
 */
public class AccessToken {
	// id
	private int id;
	// 获取到的凭证
	private String token;
	// 凭证有效期，单位：秒
	// private Integer expiresIn;
	// 上次获得token的时间
	private long time;

	private static int EXPIRES_IN = 7200;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	// public Integer getExpiresIn() {
	// return expiresIn;
	// }
	//
	// public void setExpiresIn(Integer expiresIn) {
	// this.expiresIn = expiresIn;
	// }

	public boolean validate() {

		if (time != 0) {
			Calendar calendar = Calendar.getInstance();
			java.sql.Timestamp curTime = new java.sql.Timestamp(
					calendar.getTimeInMillis());

			int diff = (int) (curTime.getTime() - time) / 1000;

			if (diff < EXPIRES_IN && diff > 0) {
				// 有效范围内
				return true;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		sb.append("-----AcceccToken-----\n");
		sb.append("id    : " + id + "\n");
		sb.append("token : " + token + "\n");
		sb.append("time  : " + time + "\n");
		sb.append("---------------------");

		return sb.toString();
	}
}